//
//  MoveCellViewController.m
//  CustomDemo
//
//  Created by 余意 on 2016/12/20.
//  Copyright © 2016年 余意. All rights reserved.
//

#import "MoveCellViewController.h"

#import "MoveCell.h"

@interface MoveCellViewController () <UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong) UICollectionView * collectionView;

@property (nonatomic,strong) UICollectionViewFlowLayout * flowLayout;

@property (nonatomic,strong) NSMutableArray * dataArray;

@property (nonatomic,strong) UILongPressGestureRecognizer * longPress;


@end

@implementation MoveCellViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self.collectionView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Lazy Load
- (NSMutableArray *)dataArray
{
    if (!_dataArray)
    {
        _dataArray = [NSMutableArray arrayWithCapacity:30];
        for (NSInteger i = 0 ; i < 30 ; i ++)
        {
            NSString * string = [NSString stringWithFormat:@"%ld",i];
            [_dataArray addObject:string];
        }
    }
    return _dataArray;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        _flowLayout = [[UICollectionViewFlowLayout alloc]init];
        _flowLayout.minimumLineSpacing = 10 ;
        _flowLayout.minimumInteritemSpacing = 10 ;
        _flowLayout.sectionInset = UIEdgeInsetsMake(10, 5, 10, 5);
        _flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 30) / 3.0, 50);
        
        _collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:_flowLayout];
        _collectionView.backgroundColor = RANDOMCOLOR;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [self.view addSubview:_collectionView];
        [_collectionView registerClass:[MoveCell class] forCellWithReuseIdentifier:@"MoveCell"];
        
        //长按手势
        _longPress = [[UILongPressGestureRecognizer alloc]init];
        [_longPress addTarget:self action:@selector(longPressTap:)];
        [_collectionView addGestureRecognizer:_longPress];
    }
    return _collectionView;
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    MoveCell * cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"MoveCell" forIndexPath:indexPath];
    cell.lable.text = self.dataArray[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    MoveCell * cell = (MoveCell *)[self.collectionView cellForItemAtIndexPath:destinationIndexPath];
    cell.contentView.backgroundColor = RANDOMCOLOR;
    
    NSString * string = [self.dataArray objectAtIndex:sourceIndexPath.item];
    [self.dataArray removeObject:string];
    [self.dataArray insertObject:string atIndex:destinationIndexPath.item];
    
    /*
    NSIndexPath * selectIndexPath = [self.collectionView indexPathForItemAtPoint:[_longPress locationInView:self.collectionView]];
    
    NSString * string = [self.dataArray objectAtIndex:selectIndexPath.item];
    NSLog(@"%@",string);
    [self.dataArray removeObject:string];
    [self.dataArray insertObject:string atIndex:destinationIndexPath.item];
    
    [self.collectionView reloadData];
     */
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",indexPath);
}

- (void)longPressTap:(UILongPressGestureRecognizer *)tap
{
    switch (tap.state)
    {
        case UIGestureRecognizerStateBegan:
        {
            NSIndexPath * selectIndexPath = [self.collectionView indexPathForItemAtPoint:[tap locationInView:self.collectionView]];
//            MoveCell * cell = (MoveCell *)[self.collectionView cellForItemAtIndexPath:selectIndexPath];
            [self.collectionView beginInteractiveMovementForItemAtIndexPath:selectIndexPath];
            break;
        }
            
        case UIGestureRecognizerStateChanged:
            [self.collectionView updateInteractiveMovementTargetPosition:[tap locationInView:tap.view]];
            break;
        
        case UIGestureRecognizerStateEnded:
            [self.collectionView endInteractiveMovement];
            break;
            
        default:
            [self.collectionView cancelInteractiveMovement];
            break;
    }
}






























@end
